/*     */ package com.newcrm.util;
/*     */ 
/*     */ import com.newcrm.excel.AbstractExcelReader;
/*     */ import com.newcrm.excel.XLSReader;
/*     */ import com.newcrm.excel.XLSXReader;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*     */ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
/*     */ import org.apache.poi.ss.usermodel.Cell;
/*     */ import org.apache.poi.ss.usermodel.CreationHelper;
/*     */ import org.apache.poi.ss.usermodel.Font;
/*     */ import org.apache.poi.ss.usermodel.RichTextString;
/*     */ import org.apache.poi.ss.usermodel.Row;
/*     */ import org.apache.poi.ss.usermodel.Sheet;
/*     */ import org.apache.poi.ss.usermodel.Workbook;
/*     */ import org.apache.poi.ss.usermodel.WorkbookFactory;
/*     */ 
/*     */ public class ExcelUtil
/*     */ {
/*     */   private static final String EXCEL_XLSX = "xlsx";
/*  33 */   private static Logger logger = Logger.getLogger(ExcelUtil.class.getName());
/*     */ 
/*     */   private static String getFileExtension(File file)
/*     */   {
/*  52 */     if ((file == null) || (file.length() == 0L)) {
/*  53 */       return null;
/*     */     }
/*  55 */     String fileName = file.getName();
/*  56 */     int idx = fileName.lastIndexOf('.');
/*  57 */     if ((idx > -1) && (idx < fileName.length())) {
/*  58 */       return fileName.substring(idx + 1);
/*     */     }
/*  60 */     return null;
/*     */   }
/*     */ 
/*     */   public static List<String[]> readExcel(File file)
/*     */     throws Exception
/*     */   {
/*  71 */     AbstractExcelReader reader = null;
/*  72 */     if ("xlsx".equalsIgnoreCase(getFileExtension(file)))
/*  73 */       reader = new XLSXReader(file);
/*     */     else {
/*  75 */       reader = new XLSReader(file);
/*     */     }
/*  77 */     return reader.parse();
/*     */   }
/*     */ 
/*     */   public static void export(String title, String[] head, String[] headKey, List<Map<String, Object>> list, Map<String, Map<String, String>> mapping, OutputStream out)
/*     */   {
/*  94 */     if (list == null) {
/*  95 */       logger.warn("list is null");
/*  96 */       return;
/*     */     }
/*     */ 
/*  99 */     if (out == null) {
/* 100 */       logger.warn("out is null");
/* 101 */       return;
/*     */     }
/*     */ 
/* 104 */     Workbook wb = new HSSFWorkbook();
/* 105 */     CreationHelper creationHelper = wb.getCreationHelper();
/* 106 */     Sheet sheet = wb.createSheet(replaceSheetTitle(title));
/*     */ 
/* 109 */     Row headRow = sheet.createRow(0);
/* 110 */     Font font = wb.createFont();
/* 111 */     font.setBoldweight((short)700);
/*     */ 
/* 114 */     Cell numberCell = headRow.createCell(0);
/* 115 */     RichTextString numberText = creationHelper.createRichTextString("编号");
/* 116 */     numberText.applyFont(font);
/* 117 */     numberCell.setCellValue(numberText);
/*     */ 
/* 119 */     for (int i = 0; i < head.length; i++) {
/* 120 */       Cell cell = headRow.createCell(i + 1);
/* 121 */       RichTextString str = creationHelper.createRichTextString(head[i]);
/* 122 */       str.applyFont(font);
/* 123 */       cell.setCellValue(str);
/*     */     }
/*     */ 
/* 127 */     int i = 1;
/* 128 */     for (Map objs : list) {
/* 129 */       Row row = sheet.createRow(i);
/* 130 */       row.createCell(0).setCellValue(i);
/* 131 */       for (int j = 0; j < headKey.length; j++) {
/* 132 */         Cell cell = row.createCell(j + 1);
/* 133 */         Object obj = objs.get(headKey[j]);
/* 134 */         if (obj != null) {
/* 135 */           String value = obj.toString();
/* 136 */           cell.setCellValue(getMappingValue(headKey[j], value, 
/* 137 */             mapping));
/*     */         }
/*     */       }
/* 140 */       i++;
/*     */     }
/*     */     try
/*     */     {
/* 144 */       wb.write(out);
/*     */     } catch (IOException e) {
/* 146 */       logger.warn("export excel faild", e);
/*     */       try
/*     */       {
/* 149 */         out.close();
/*     */       } catch (IOException se) {
/* 151 */         logger.warn("export excel faild", se);
/*     */       }
/*     */     }
/*     */     finally
/*     */     {
/*     */       try
/*     */       {
/* 149 */         out.close();
/*     */       } catch (IOException e) {
/* 151 */         logger.warn("export excel faild", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void addData(String filePath, String[] headKey, List<Map<String, Object>> list, Map<String, Map<String, String>> mapping, int idx)
/*     */   {
/* 167 */     if (list == null) {
/* 168 */       return;
/*     */     }
/*     */ 
/* 171 */     Workbook wb = null;
/*     */     try {
/* 173 */       InputStream inp = new FileInputStream(filePath);
/* 174 */       wb = WorkbookFactory.create(inp);
/*     */     } catch (IOException|InvalidFormatException e) {
/* 176 */       logger.warn("open excel file faild", e);
/* 177 */       return;
/*     */     }
/*     */ 
/* 180 */     Sheet sheet = wb.getSheetAt(0);
/*     */ 
/* 183 */     int i = idx + 1;
/* 184 */     for (Map objs : list) {
/* 185 */       Row row = sheet.createRow(i);
/* 186 */       row.createCell(0).setCellValue(i);
/* 187 */       for (int j = 0; j < headKey.length; j++) {
/* 188 */         Cell cell = row.createCell(j + 1);
/* 189 */         Object obj = objs.get(headKey[j]);
/* 190 */         if (obj != null) {
/* 191 */           String value = obj.toString();
/* 192 */           cell.setCellValue(getMappingValue(headKey[j], value, 
/* 193 */             mapping));
/*     */         }
/*     */       }
/* 196 */       i++;
/*     */     }
/*     */ 
/* 199 */     FileOutputStream fileOut = null;
/*     */     try {
/* 201 */       fileOut = new FileOutputStream(filePath);
/* 202 */       wb.write(fileOut);
/*     */     }
/*     */     catch (IOException e) {
/* 205 */       logger.warn("export excel faild", e);
/*     */ 
/* 207 */       if (fileOut != null)
/*     */         try {
/* 209 */           fileOut.close();
/*     */         } catch (IOException se) {
/* 211 */           logger.warn("export excel faild", se);
/*     */         }
/*     */     }
/*     */     finally
/*     */     {
/* 207 */       if (fileOut != null)
/*     */         try {
/* 209 */           fileOut.close();
/*     */         } catch (IOException e) {
/* 211 */           logger.warn("export excel faild", e);
/*     */         }
/*     */     }
/*     */   }
/*     */ 
/*     */   private static String getMappingValue(String title, String value, Map<String, Map<String, String>> mapping)
/*     */   {
/* 221 */     if ((value == null) || (mapping == null)) {
/* 222 */       return value;
/*     */     }
/*     */ 
/* 225 */     Map mappingValues = (Map)mapping.get(title);
/* 226 */     if (mappingValues == null) {
/* 227 */       return value;
/*     */     }
/* 229 */     String mapValue = (String)mappingValues.get(value);
/*     */ 
/* 232 */     if ((mapValue != null) && (mapValue.length() >= 32767)) {
/* 233 */       return mapValue.substring(0, 32765);
/*     */     }
/* 235 */     return mapValue;
/*     */   }
/*     */ 
/*     */   private static String replaceSheetTitle(String title) {
/* 239 */     if ((title == null) || (title.length() == 0)) {
/* 240 */       return title;
/*     */     }
/*     */ 
/* 243 */     StringBuffer v = new StringBuffer();
/* 244 */     for (int i = 0; i < title.length(); i++) {
/* 245 */       char t = title.charAt(i);
/* 246 */       if (!isIllegalChar(t)) {
/* 247 */         v.append(t);
/*     */       }
/*     */     }
/* 250 */     return v.toString();
/*     */   }
/*     */ 
/*     */   private static boolean isIllegalChar(char t) {
/* 254 */     char[] chars = { '*', ':', '[', ']', '\\', '/', '?' };
/*     */ 
/* 256 */     for (int i = 0; i < chars.length; i++) {
/* 257 */       if (chars[i] == t) {
/* 258 */         return true;
/*     */       }
/*     */     }
/*     */ 
/* 262 */     return false;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.util.ExcelUtil
 * JD-Core Version:    0.6.1
 */